package com.cli.idempotent.controller;

import com.cli.idempotent.annotation.AutoIdempotent;
import com.cli.idempotent.constant.ResponseCode;
import com.cli.idempotent.model.ResultVo;
import com.cli.idempotent.service.TestService;
import com.cli.idempotent.service.TokenService;
import com.cli.idempotent.util.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/10/31 2:39 下午
 * @see
 */
@RestController
public class BusinessController {


    @Resource
    private TokenService tokenService;

    @Resource
    private TestService testService;


    @PostMapping("/get/token")
    public String  getToken() throws JsonProcessingException {
        String token = tokenService.createToken();
        if (StringUtils.isNotBlank(token)) {
            ResultVo resultVo = new ResultVo();
            resultVo.setCode(ResponseCode.code_success);
            resultVo.setMessage(ResponseCode.SUCCESS);
            resultVo.setData(token);
            return JSONUtil.toJsonStr(resultVo);
        }
        return StringUtils.EMPTY;
    }


    @AutoIdempotent
    @PostMapping("/test/Idempotence")
    public String testIdempotence() throws JsonProcessingException {
        String businessResult = testService.testIdempotence();
        if (StringUtils.isNotBlank(businessResult)) {
            ResultVo successResult = ResultVo.getSuccessResult(businessResult);
            return JSONUtil.toJsonStr(successResult);
        }
        return StringUtils.EMPTY;
    }
}
